package com.salton123.qa.demo

import android.Manifest
import android.content.Intent
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.qw.soul.permission.SoulPermission
import com.qw.soul.permission.bean.Permission
import com.qw.soul.permission.callbcak.CheckRequestPermissionListener
import kotlinx.android.synthetic.main.activity_main.*

val TAG = "MainActivity"

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        tvCrashTest.setOnClickListener {
            Log.i(TAG, "hello time=${System.currentTimeMillis()}")
            Hello().sayHello()
            throw  RuntimeException("hello")
        }
        tvBlockTest.setOnClickListener {
            startActivity(Intent(this@MainActivity, TestBlockAty::class.java))
        }
        tvLeakTest.setOnClickListener {
            startActivity(Intent(this@MainActivity, TestLeakAty::class.java))
        }
        tvInfoPanel.setOnClickListener {

        }
        tvAnrPanel.setOnClickListener {
            startActivity(Intent(this@MainActivity, TestAnrAty::class.java))
        }
        SoulPermission.getInstance().checkAndRequestPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE,
            object : CheckRequestPermissionListener {
                override fun onPermissionOk(permission: Permission?) {
                    Log.i(TAG, "onPermissionOk:$permission")
                }

                override fun onPermissionDenied(permission: Permission?) {
                    Log.i(TAG, "onPermissionDenied:$permission")
                }
            })
        tvUiDebug.setOnClickListener {

        }
        tvImageDetect.setOnClickListener {
            startActivity(Intent(this@MainActivity, ImageDetectActivity::class.java))
        }
    }


    /**
     * 检查包是否存在 * * @param packname * @return
     */
    private fun checkPackInfo(packname: String): Boolean {
        var packageInfo: PackageInfo? = null
        try {
            packageInfo = packageManager.getPackageInfo(packname, 0)
        } catch (e: PackageManager.NameNotFoundException) {
            e.printStackTrace()
        }

        return packageInfo != null
    }

    fun startAppByPackage(packageName: String) {
        try {
            val packageManager = packageManager
            if (checkPackInfo(packageName)) {
                val intent = packageManager.getLaunchIntentForPackage(packageName)
                intent!!.flags = Intent.FLAG_ACTIVITY_NEW_TASK
                startActivity(intent)
            } else {
                Toast.makeText(this, "没有安装$packageName", Toast.LENGTH_LONG).show()
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.i(TAG, "life click")
    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        Log.i(TAG, "life click")
    }

    class Hello {
        fun sayHello() {
            Log.i(TAG, "hello")
        }
    }
}
